package com.gitee.cashzhang27.test.cloud.seata.storage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.cashzhang27.test.cloud.seata.storage.entity.StorageTbl;
import com.gitee.cashzhang27.test.cloud.seata.storage.mapper.StorageMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description：
 *
 * @author fangliangsheng
 * @date 2019-04-04
 */
@Service
@AllArgsConstructor
public class StorageService {

  private StorageMapper storageDAO;

  @Transactional
  public void deduct(String commodityCode, int count) {
    StorageTbl storageTbl = StorageTbl.builder().commodityCode(commodityCode).build();
    StorageTbl storage = storageDAO.selectOne(new QueryWrapper<>(storageTbl));
    storage.setCount(storage.getCount() - count);

    storageDAO.updateById(storage);
  }
}
